﻿using SuperMap.Data;
using SuperMap.Realspace;
using SuperMap.UI;
using System;
using WeifenLuo.WinFormsUI.Docking;
using Ycjw.PipeLine.Map.Basic.Conditions;
using Ycjw.PipeLine.Map.Basic.SuperMap;

namespace Ycjw.PipeLine.Map.Tools.Conditions
{
    /// <summary>
    /// 空间查询
    /// </summary>
    public class LayerSpatialQuery : ConditionPanel
    {
        public override string ConditionPanelName { get { return "空间查询"; } set => base.ConditionPanelName = value; }
        private Sunny.UI.UIPanel uiPanelLeft;
        private Sunny.UI.UIGroupBox uiGroupBoxRight;
        private LayerWithDataListPanel m_LayerWithDataListPanel;

        public LayerSpatialQuery():base()
        {
            InitializeComponent();
        }
        private void AddSqlConditions()
        {
            Sunny.UI.UIRadioButton rb1 = new Sunny.UI.UIRadioButton()
            {
                Text = "矩形查询",
                Name = "RetacleQuery",
                Location = new System.Drawing.Point(20, 40)
            };
            Sunny.UI.UIRadioButton rb2 = new Sunny.UI.UIRadioButton()
            {
                Text = "圆形查询",
                Name = "CycleQuery",
                Location = new System.Drawing.Point(20, 68)
            };
            Sunny.UI.UIRadioButton rb3 = new Sunny.UI.UIRadioButton()
            {
                Text = "多边形查询",
                Name = "PolygonQuery",
                Location = new System.Drawing.Point(20, 96)
            };
            uiGroupBoxRight.Controls.Add(rb1);
            uiGroupBoxRight.Controls.Add(rb2);
            uiGroupBoxRight.Controls.Add(rb3);
            rb1.ValueChanged += ValueChanged;
            rb2.ValueChanged += ValueChanged;
            rb3.ValueChanged += ValueChanged;            
        }

        private void SceneControl_Tracking(object sender, Tracking3DEventArgs e)
        {
            if (Action3D.CreateLine != this.SceneControl.Action) return;
            var tag = "cycle_tmp";
            var index = Scene.TrackingLayer.IndexOf(tag);
            if (index > -1)
            {
                Scene.TrackingLayer.Remove(index);
            }
            GeoLine3D line3d = e.Geometry as GeoLine3D;
            if (line3d == null || line3d.Length == 0)
            {
                return;
            }
            var ps = line3d[0];
            var length = Math.Sqrt(Math.Pow((ps[0].X - ps[1].X), 2) + Math.Pow((ps[0].Y - ps[1].Y), 2));
            Point2D p2d = new Point2D(ps[0].X, ps[0].Y);
            GeoRectangle gr = new GeoRectangle(p2d, length * 2, length * 2, 0);
            GeoEllipse ge = new GeoEllipse(gr);
            var gReg = ge.ConvertToRegion(40);
            var p3ds = new Point3Ds();
            for (int i = 0; i < gReg[0].Count; i++)
            {
                p3ds.Add(new Point3D(gReg[0][i].X, gReg[0][i].Y, 0));
            }
            var gReg3d = new GeoRegion3D(p3ds)
            {
                Style3D = new GeoStyle3D()
                {
                    LineColor = System.Drawing.Color.Yellow,
                    LineWidth = 2,
                    FillBackColor = System.Drawing.Color.FromArgb(120, System.Drawing.Color.Yellow),
                    FillForeColor = System.Drawing.Color.FromArgb(120, System.Drawing.Color.Yellow),
                    AltitudeMode = AltitudeMode.ClampToGround
                }
            };
            Scene.TrackingLayer.Add(gReg3d, tag);
        }
        private void SceneControl_Tracked(object sender, Tracked3DEventArgs e)
        {
            var geom = e.Geometry;
            if (geom == null) return;
            if(geom.Type == SuperMap.Data.GeometryType.GeoLine3D)
            {
            }
            else
            {
                geom.Style3D = new GeoStyle3D()
                {
                    LineColor = System.Drawing.Color.Yellow,
                    LineWidth = 2,
                    FillBackColor = System.Drawing.Color.FromArgb(120, System.Drawing.Color.Yellow),
                    FillForeColor = System.Drawing.Color.FromArgb(120, System.Drawing.Color.Yellow),
                    AltitudeMode = AltitudeMode.ClampToGround
                };
                Scene.TrackingLayer.Add(geom, string.Empty);
            }
        }

        public override bool ConditionValid()
        {
            if (m_LayerWithDataListPanel == null || m_LayerWithDataListPanel.SelectLayer3DDataset == null)
            {
                Sunny.UI.UIMessageTip.ShowError("未获取到选中图层");
                IsConditionValid = false;
                return false;
            }

            if (!(m_LayerWithDataListPanel.SelectLayer3DDataset.Dataset is DatasetVector)) return false;

            if (Scene.TrackingLayer.Count == 0)
            {
                Sunny.UI.UIMessageTip.ShowError("未指定查询方式或者未进行绘制");
                IsConditionValid = false;
                return false;
            }
            return base.ConditionValid();
        }

        private void ValueChanged(object sender, bool value)
        {
            SceneControl sceneControl = this.SceneControl;
            if (sceneControl == null) return;
            Scene.TrackingLayer.Clear();
            Sunny.UI.UIRadioButton btn = sender as Sunny.UI.UIRadioButton;
            if (!btn.Checked) return;
            if(btn.Name == "RetacleQuery")
            {
                sceneControl.Action = Action3D.CreateRectangle;
            }
            else if (btn.Name == "CycleQuery")
            {
                sceneControl.Action = Action3D.CreateLine;
            }
            else if (btn.Name == "PolygonQuery")
            {
                sceneControl.Action = Action3D.CreatePolygon;
            }
            sceneControl.Refresh();
            sceneControl.Tracked += SceneControl_Tracked;
            sceneControl.Tracking += SceneControl_Tracking;
        }

        public override void AfterClose()
        {
            SceneControl sceneControl = this.SceneControl;
            if (sceneControl == null) return;
            sceneControl.Action = Action3D.Pan;
            sceneControl.Tracked -= SceneControl_Tracked;
            sceneControl.Tracking -= SceneControl_Tracking;
            Scene.TrackingLayer.Clear();
        }

        protected override void OnLoad(EventArgs e)
        {
            m_LayerWithDataListPanel = new LayerWithDataListPanel()
            {
                Dock = System.Windows.Forms.DockStyle.Fill
            };
            Scene scene = this.Scene;
            if (scene == null) return;
            var types = new System.Collections.Generic.List<DatasetType>()
            {
                DatasetType.Line,
                DatasetType.Line3D,
                DatasetType.Network,
                DatasetType.Network3D
            };
            m_LayerWithDataListPanel.Init(scene);
            m_LayerWithDataListPanel.LoadTo(types);
            //this.uiPanelLeft.Padding = new System.Windows.Forms.Padding(0, 0, 0, 0);
            this.uiPanelLeft.Controls.Add(m_LayerWithDataListPanel);
            AddSqlConditions();

            this.SceneControl.ActionChanged += SceneControl_ActionChanged;
        }

        private void SceneControl_ActionChanged(object sender, Action3DChangedEventArgs e)
        {
            Console.WriteLine(this.SceneControl.Action);
        }

        private void InitializeComponent()
        {
            this.uiGroupBoxRight = new Sunny.UI.UIGroupBox();
            this.uiPanelLeft = new Sunny.UI.UIPanel();
            this.SuspendLayout();
            // 
            // uiGroupBoxRight
            // 
            this.uiGroupBoxRight.Dock = System.Windows.Forms.DockStyle.Fill;
            this.uiGroupBoxRight.Font = new System.Drawing.Font("微软雅黑", 12F);
            this.uiGroupBoxRight.Location = new System.Drawing.Point(247, 0);
            this.uiGroupBoxRight.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
            this.uiGroupBoxRight.MinimumSize = new System.Drawing.Size(1, 1);
            this.uiGroupBoxRight.Name = "uiGroupBoxRight";
            this.uiGroupBoxRight.Padding = new System.Windows.Forms.Padding(0, 32, 0, 0);
            this.uiGroupBoxRight.Size = new System.Drawing.Size(247, 319);
            this.uiGroupBoxRight.TabIndex = 1;
            this.uiGroupBoxRight.Text = "空间查询方式";
            // 
            // uiPanelLeft
            // 
            this.uiPanelLeft.Dock = System.Windows.Forms.DockStyle.Left;
            this.uiPanelLeft.Font = new System.Drawing.Font("微软雅黑", 12F);
            this.uiPanelLeft.Location = new System.Drawing.Point(0, 0);
            this.uiPanelLeft.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
            this.uiPanelLeft.MinimumSize = new System.Drawing.Size(1, 1);
            this.uiPanelLeft.Name = "uiPanelLeft";
            this.uiPanelLeft.Size = new System.Drawing.Size(247, 319);
            this.uiPanelLeft.TabIndex = 2;
            // 
            // LayerSpatialQuery
            // 
            this.Controls.Add(this.uiGroupBoxRight);
            this.Controls.Add(this.uiPanelLeft);
            this.Name = "LayerSpatialQuery";
            this.Size = new System.Drawing.Size(494, 319);
            this.ResumeLayout(false);

        }
        public override object Do(DockPanel dockPanel = null)
        {
            if (m_LayerWithDataListPanel == null || m_LayerWithDataListPanel.SelectLayer3DDataset == null) 
            {
                IsConditionValid = false;
                Sunny.UI.UIMessageTip.ShowError("未获取到选中图层");
                return null;
            }

            if (!(m_LayerWithDataListPanel.SelectLayer3DDataset.Dataset is DatasetVector dsv)) return null;
            DatasetVectorEx dvEx = new DatasetVectorEx(m_LayerWithDataListPanel.SelectLayer3DDataset.Name)
            {
                DatasetVector = dsv
            };
            if (Scene.TrackingLayer.Count == 0)
            {
                IsConditionValid = false;
                Sunny.UI.UIMessageTip.ShowError("未指定查询方式或者未进行绘制");
                return null;
            }
            var geom = Scene.TrackingLayer.Get(0);
            if (geom is GeoRegion3D) 
                geom = (geom as GeoRegion3D).ConvertToRegion();
            return dvEx.Query(string.Empty, geom);            
        }
    }
}
